<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license        
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Website
 *
 * Allows to manage website informations
 *
 * @package         conlabz CMS
 * @subpackage      Controllers
 */
class Website extends Admin_Controller {
	
	function Website( ) {
		parent::Admin_Controller( );
		
		$this->load->helper( 'form' );

		// Check for access permission
		// needs to be defined as a resource over the admin area
		check( 'Website' );

		// Load the validation library
		$this->load->library( 'validation' );

		// Load the website model
		$this->load->model( 'website_model' );
		
		// Load the language files
		$this->lang->module_load( 'article', 'website' );
		$this->lang->module_load( 'article', 'classification' );
		$this->lang->module_load( 'article', 'domain' );
		$this->lang->module_load( 'article', 'language' );
		$this->lang->module_load( 'article', 'portlets' );
		
		// Set breadcrumb
		$this->bep_site->set_crumb( $this->lang->line( 'website' ), 'article/admin/website' );

		log_message( 'debug', 'conlabz CMS : Website class loaded' );
	}	
	
	/**
	 * View Websites
	 *
	 * @access public
	 */
	function index( ) {
		// Get Website Infomation
		$data['websites'] = array( );
		$data['websites'] = $this->website_model->getWebsites( );

		// Display Page
		$data['header'] = $this->lang->line( 'website_overview' );
		$data['page'] = $this->config->item('backendpro_template_admin') . "/view_website";
		$data['module'] = 'article';
		$this->load->view( $this->_container, $data );
	}
	
	function manage( ){
		//@todo: does it work and make sense?
		$website_id = $this->uri->segment( 6 );
		$website = $this->website_model->getWebsiteInfosByUserId( $website_id, true );
		$this->session->set_userdata( $website );
		redirect( $this->config->item('userlib_action_login'), 'location' );
	}
	
	/**
	 * Set Website Defaults
	 *
	 * Specify what values should be shown in the fields when creating
	 * a new website by default
	 *
	 * @access private
	 * @todo specify the fields
	 */
	function _set_website_defaults( ){
		//$this->validation->set_default_value('field1','value');
		//$this->validation->set_default_value('field2','value');
	}

	/**
	 * Get Website Details
	 *
	 * Load user detail values from the submited form
	 *
	 * @access private
	 * @return array
	 */
	function _get_website_details() {
		$data['id'] = $this->input->post( 'id' );
		$data['name'] = $this->input->post( 'name' );
		$data['template'] = $this->input->post( 'template' );
		$data['language'] = $this->input->post( 'language' );
		$data['domains'] = $this->input->post( 'domains' );
		$data['active'] = $this->input->post( 'active' );
		$data['classification_id'] = $this->input->post( 'classification_id' );
		$data['user_id'] = $this->input->post( 'user_id' );
		return $data;
	}
	
	/**
	 * Display Website Form
	 *
	 * @access public
	 * @param integer $id Website ID
	 */
	function form( $id = NULL ) {    
		// VALIDATION FIELDS
		$fields['id'] = "ID";
		$fields['name'] = $this->lang->line( 'website_title' );
		$fields['template'] = $this->lang->line( 'website_template' );
		$fields['language'] = $this->lang->line( 'website_language' );
		$fields['domains'] = $this->lang->line( 'website_domains' );
		$fields['active'] = $this->lang->line( 'website_active' );
		$fields['classification_id'] = $this->lang->line('website_classification_id');
		$this->validation->set_fields( $fields );

		// Setup validation rules
		if( is_null( $id ) ) {
			// Use create website rules (make sure no-one has the same short name)
			$rules['name'] = "trim|required";
			$rules['template'] = "trim|required";
			$rules['language'] = "trim|required";
			$rules['domains'] = "trim|required";
			//$rules['classification_id'] = "trim|required";
		} else {                                  
			// Use edit website rules (make sure no-one other than the current user has the same short name)
			$rules['name'] = "trim|required";
			$rules['template'] = "trim|required";
			$rules['language'] = "trim|required";
			$rules['domains'] = "trim|required";
			//$rules['classification_id'] = "trim|required";
		}
		
		// Setup form default values
		if( !is_null( $id ) AND !$this->input->post( 'submit' ) ){
			// Modify form, first load
			$website = $this->website_model->getWebsites( array( 'website.id' => $id ) );
			$website = $website->row_array( );
			$this->validation->set_default_value( 'category', $website['classification_id'] );
			$this->validation->set_default_value( $website );
		} elseif( is_null( $id ) AND !$this->input->post( 'submit' ) ){
			// Create form, first load
			$this->validation->set_default_value( 'category', $this->preference->item( 'default_website_category' ) );
			$this->validation->set_default_value( 'active', '1' );
			// Setup website defaults
			$this->_set_website_defaults();
		} elseif( $this->input->post('submit')){
			// Form submitted, check rules
			$this->validation->set_rules( $rules );
		}

		// RUN
		if( $this->validation->run( ) === FALSE) {
			// Construct Classification dropdown
			$this->load->model( 'classification_model' );
			$data['classification'] = $this->classification_model->buildClassificationDropdown( );
			
			// Construct Language dropdown
			$this->load->model( 'domain_model' );
			$data['domains'] = $this->domain_model->buildDomainDropdown( $id );
			
			// Construct Language dropdown
			$this->load->model( 'language_model' );
			$data['language'] = $this->language_model->buildLanguageDropdown( );
			
			// Construct User dropdown
			$this->load->model( 'user_model' );
			$user = $this->user_model->getUsers( );
			foreach( $user->result_array( ) as $row ){
				$data['user'][$row['id']] = $row['email'];
			} //foreach
			
			// Display form
			$this->validation->output_errors();
			$data['header'] = (is_null( $id ) ? $this->lang->line( 'create_website' ) : $this->lang->line( 'edit_website' ) );
			$this->bep_site->set_crumb( $data['header'],'article/admin/website/form/'.$id );
			$data['page'] = $this->config->item( 'backendpro_template_admin' ) . "/form_website";
			$data['module'] = 'article';
			$this->load->view( $this->_container, $data );
		} else {
			// Save form
			if( is_null( $id ) ) {
				// CREATE
				// Fetch form values
				$website = $this->_get_website_details( );
			
				$this->db->trans_start( );
				$website['website_id'] = $this->website_model->insert( $website );
				
				// Insert domain data
				$this->load->model( 'domain_model' );
				$this->domain_model->insert( $website );
				
				if( $this->db->trans_status( ) === TRUE ){
					$this->db->trans_commit();
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $website['name'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line('backendpro_action_failed'), $this->lang->line('userlib_create_user') ) );
				}
				redirect('article/admin/website');
			} else {
				// SAVE
				$website = $this->_get_website_details( );
			
				$this->db->trans_start();
				$this->website_model->update( $website, $website['id'] );

				if( $this->db->trans_status( ) === TRUE ) {
					$this->db->trans_commit( );
					flashMsg( 'success', sprintf( $this->lang->line( 'userlib_user_saved' ), $website['name'] ) );
				} else {
					$this->db->trans_rollback( );
					flashMsg( 'error', sprintf( $this->lang->line( 'backendpro_action_failed' ), $this->lang->line( 'userlib_edit_user' ) ) );
				}
				redirect('article/admin/website');
			}
		}	
	} // form( );
	
	/**
	 * Delete Websites
	 *
	 * @access public
	 */
	function delete( ) {
		$website_id = $this->input->post( 'id' );		
		$this->load->model('domain_model');	
		$this->domain_model->delete( $website_id );	
		
		if( FALSE === ( $selected = $this->input->post( 'select' ) ) ) {
			redirect( 'article/admin/website', 'location' );
		}
		
		foreach( $selected as $website ) {
			$this->db->delete( 'website', array( 'id' => $website ) );
		}
		
		flashMsg( 'success', $this->lang->line( 'website_deleted' ) );
		redirect( 'article/admin/website', 'location' );	
	}
}
/* End of file website.php */
/* Location: ./modules/article/controllers/admin/website.php */
